package initialize

import (
	"fmt"
	"user_api/global"

	_ "github.com/mbobakov/grpc-consul-resolver"
	"go.uber.org/zap"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"

	pb "user_api/proto"
)

func InitRpcConn() {
	conn, err := grpc.Dial(
		fmt.Sprintf("consul://%s:%d/%s?wait=14s&tag=%s", global.ServerConfig.Consul.Host, global.ServerConfig.Consul.Port, global.ServerConfig.UserRpc.Name, global.ServerConfig.UserRpc.Tag), grpc.WithTransportCredentials(insecure.NewCredentials()),
		grpc.WithDefaultServiceConfig(`{"loadBalancingPolicy": "round_robin"}`),
	)
	if err != nil {
		zap.L().Panic("连接consul失败", zap.Error(err))
		return
	}
	global.UserRpcClient = pb.NewUserClient(conn)
}
